package it.greg.findparty.android.ui;

import it.greg.findparty.R;
import it.greg.findparty.android.util.SharedObjects;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Spinner;

public class SettingsActivity extends Activity{
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		//Load saved settings
		String city = SharedObjects.settingSaver.getSettingString("city");
		if (city!=null){
			Spinner spinCity = (Spinner) findViewById(R.id.spinCity);
			String[] cities = getResources().getStringArray(R.array.city_array);
			for (int i=0; i<cities.length;i++){
				if (city.equals(cities[i]))
					spinCity.setSelection(i);
			}
		}
		String time = SharedObjects.settingSaver.getSettingString("time");
		if (time!=null){
			Spinner spinTime = (Spinner) findViewById(R.id.spinTime);
			String[] times = getResources().getStringArray(R.array.time_array);
			for (int i=0; i<times.length;i++){
				if (time.equals(translateTimeToUri(times[i])))
					spinTime.setSelection(i);
			}
		}
	}
	
	public void btnSaveSettings_click(View v){
		Spinner spinCity = (Spinner) findViewById(R.id.spinCity);
		String city = spinCity.getSelectedItem().toString();
		SharedObjects.settingSaver.saveSettingString("city", city);
		Spinner spinTime = (Spinner) findViewById(R.id.spinTime);
		String time = spinTime.getSelectedItem().toString();
		SharedObjects.settingSaver.saveSettingString("time", translateTimeToUri(time));
		this.finish(); //Go back to previous activity
	}
	
	private String translateTimeToUri(String time){
		String result = null;
		if (time.equals("Tutte le ore")){
			result = "orario_all";
		}
		else if (time.equals("Fino alle 15:00")){
			result = "orario_fino_15";
		}
		else if (time.equals("Dalle 15:00 alle 20:00")){
			result = "orario_dalle_15_alle_20";
		}
		else if (time.equals("Dalle 20:00 in poi")){
			result = "orario_dopo_le_20";
		}
		return result;
	}
}
